'''
Created on 09.10.2010
prepare psl files for any task for file_backup
@author: ipse
'''
import os
from os.path import join, getsize
import auto_utility
import shutil
PATH_CONFIG = r'E:\py\test_suites\BF_RF_with_pass\real_32.ini'
PATH_TO_RELEASE = r'C:\0_struct_for_r11pm\folder_with_task'
STRING_TO_SEARCH = 'VM_SCRIPTS_FOLDER'
PATH_TO_FOLDER = r'C:\0_struct_for_r11pm\content_for_tasks'
#get directories name
def get_dir_not_empty2(path = PATH_TO_FOLDER):
    list = []
    for root, dirs, files in os.walk(path):
        if sum(getsize(join(root, name)) for name in files):
            if len(root.split('\\')) == 6:
                list.append(root)
    return list
#replace path
def change_path_in_config():

    list_dirs = os.listdir(PATH_TO_SCRIPTS)
    #get name of task. pars path
    task_dir_name = 'Scripts\\' + list_dirs[0].split('\\')[-1]
    try:
        file = open(PATH_CONFIG, 'r')
        lines = file.readlines()
        file.close()
    except:
        print("'can't open file" + PATH_CONFIG)
    i = 0
    for line in lines:
        if line.find(STRING_TO_SEARCH) != -1:
            lines[i] = STRING_TO_SEARCH + ' = ' + task_dir_name + '\n'
            break
        i += 1
    file = open(PATH_CONFIG, 'w')
    file.writelines(lines)
    file.close()
    return 0

#concatenate psl file in one
def concat_pls_files(PATH_TO_SCRIPTS, TASK_NAME):
    list_psl = auto_utility.files_list_from_path(PATH_TO_SCRIPTS, '.psl')['res']
    try:
        dest = open(join(PATH_TO_RELEASE, TASK_NAME + '.psl'), 'w') 
        for item in list_psl:
            try:
                sourse = open(item, 'r')
                data = sourse.read()
                sourse.close()
            except:
                print("can't open psl file" + item)
                return -1
            dest.write(data)
            dest.write('\n')    
        dest.close()
    except:
        print("can't write to dest in contact_pls_files")
        return -1              
    return 0

#concatenate txt file in one  
def concat_txt_files(PATH_TO_SCRIPTS, TASK_NAME):
    list_psl = auto_utility.files_list_from_path(PATH_TO_SCRIPTS, '.txt')['res']
    try:
        dest = open(join(PATH_TO_RELEASE, TASK_NAME + '.txt'), 'w')
        for item in list_psl:
            try:
                sourse = open(item, 'r')
                data = sourse.read()
                
                sourse.close()
            except:
                print("can't open psl file" + item)
                return - 1
            name = item.split('\\')[-1]
            dest.write('<file>\n<name>')
            dest.write(name + '')
            dest.write('</name>\n<content>\n')
            dest.write(data + '\n')
            dest.write('</content>\n</file>\n')    
        dest.close()
    except:
        print("can't write to dest in concat_txt_files")
        return - 1              
    return 0
def constract_general(PATH_TO_SCRIPTS, TASK_NAME):
    try:
        dest = open(join(PATH_TO_SCRIPTS, TASK_NAME + '.psl'), 'a')
        try:
            sourse = open(join(PATH_TO_SCRIPTS, 'start.py'), 'r')
            data = sourse.read()
            sourse.close()
        except:
            print("can't open file start.py in general")
            return - 1
        dest.write('<not scripts>\n<start.py>\n')
        dest.write(data)
        dest.write('\n</start.py>\n\n')
        dest.write('<end.py>\n')
        try:    
            sourse = open(join(PATH_TO_SCRIPTS, 'end.py'), 'r')
            data = sourse.read()
            sourse.close()
        except:
            print("can't open end.py file in general")
            return - 1
        dest.write(data)
        dest.write('\n</end.py>\n')
        dest.write('\n<data>\n')
        try:    
            sourse = open(join(PATH_TO_SCRIPTS, TASK_NAME + '.txt'), 'r')
            data = sourse.read()
            sourse.close()
        except:
            print("can't open file in general " + TASK_NAME + '.txt')
            return - 1
        dest.write(data)
        try:    
            sourse = open(join(PATH_TO_SCRIPTS, 'diskpart_command.xml'), 'r')
            data = sourse.read()
            sourse.close()
        except:
            print("can't open diskpart_command.xml file in general")
            return - 1
        dest.write('<file>\n<name>')
        dest.write('diskpart_command.xml')
        dest.write('</name>\n<content>\n')
        dest.write(data + '\n')
        dest.write('</content>\n</file>\n</data>\n</not scripts>\n')
        dest.close()
    except:
        print("can't write to dest")
        return - 1
    try:
        os.remove(join(PATH_TO_SCRIPTS, TASK_NAME + '.txt'))
    except:
        print("can't remove files or folder")
    return 0

def dirs_around():
    list_task = get_dir_not_empty2()
    for item in list_task:
        error = 0
        if item.split('\\')[5] != 'CVS':
            TASK_NAME = item.split('\\')[4] + '_' + item.split('\\')[5]
            error = concat_pls_files(item, TASK_NAME)
            if error == -1:
                return -1
            error = concat_txt_files(item, TASK_NAME)
            if error == -1:
                return -1
            error = constract_general(PATH_TO_RELEASE, TASK_NAME)
            if error == -1:
                return -1
    return 0
def delete_files():
    os.remove(join(PATH_TO_RELEASE, 'diskpart_command.xml'))
    list_py = auto_utility.files_list_from_path(PATH_TO_RELEASE, '.py')['res']
    for py in list_py:
        os.remove(join(PATH_TO_RELEASE, py))
    return 0
def copy_files():
    shutil.copy(join(PATH_TO_FOLDER, 'diskpart_command.xml'), PATH_TO_RELEASE)
    list_py = auto_utility.files_list_from_path(PATH_TO_FOLDER, '.py')['res']
    for py in list_py:
        shutil.copy(py ,PATH_TO_RELEASE)
    return 0
print (copy_files())
print (dirs_around())
print (delete_files())